using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace LlamaFarmSaveViewer
{
    public partial class Form1 : Form
    {
        string CurrentFile;
        XmlDocument xDoc = new XmlDocument();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        void LoadXML()
        {
            try
            {
                openFileDialog1.Filter = "XML Files (*.xml)|*.xml" + "|All Files|*";
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.DefaultExt = "xml";
                openFileDialog1.ShowDialog();
                openFileDialog1.Title = "Open XML File.";
                openFileDialog1.InitialDirectory = "";

                if (openFileDialog1.FileName == "")
                    return;

                CurrentFile = openFileDialog1.FileName.ToString();

                xDoc.Load(CurrentFile);

                XmlNodeList ID = xDoc.GetElementsByTagName("ID");
                XmlNodeList Gold = xDoc.GetElementsByTagName("Gold");

                LlamaIDBox.Text = ID[0].InnerText;
                GoldBox.Text = Gold[0].InnerText;
            }
            catch
            {
                return;
            }
        }

        void SaveXML()
        {
            try
            {
                StreamWriter sw = new StreamWriter(CurrentFile);

                sw.Write("<LlamaFarm> </LlamaFarm>");

                sw.Close();

                try
                {
                    xDoc.Load(CurrentFile);
                }
                catch (FileNotFoundException)
                {
                    XmlTextWriter xmlWrite = new XmlTextWriter(CurrentFile, System.Text.Encoding.UTF8);
                    xmlWrite.Formatting = Formatting.Indented;
                    xmlWrite.WriteStartElement("LlamaFarm");
                    xmlWrite.Close();
                    xDoc.Load(CurrentFile);
                }

                XmlNode root = xDoc.DocumentElement;
                XmlElement IDele = xDoc.CreateElement("ID");
                XmlElement GOLDele = xDoc.CreateElement("Gold");
                XmlElement SAVEele = xDoc.CreateElement("Save");

                XmlText IDnode = xDoc.CreateTextNode("");
                XmlText GOLDnode = xDoc.CreateTextNode("");
                XmlText SAVEnode = xDoc.CreateTextNode("");

                root.AppendChild(SAVEele);
                SAVEele.AppendChild(IDele);
                SAVEele.AppendChild(GOLDele);

                IDele.AppendChild(IDnode);
                IDnode.Value = LlamaIDBox.Text;

                GOLDele.AppendChild(GOLDnode);
                GOLDnode.Value = GoldBox.Text;

                xDoc.Save(CurrentFile);
                MessageBox.Show("Saved!", "Success");
            }
            catch(ArgumentNullException)
            {
                MessageBox.Show("Please load a save file first.");
            }
        }

        private void LoadGame_Click(object sender, EventArgs e)
        {
            LoadXML();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveXML();
        }
    }
}